﻿package
{

	import battle.BattleScene;
	
	import core.Resource.DressPack;
	import core.Resource.DressPackEvent;
	import core.Resource.DressPackManager;
	import core.Resource.InstancePool;
	import core.Resource.MapDefinition;
	import core.Resource.MapPack;
	import core.actor.core.ActorCachePolicy;
	import core.actor.core.ActorView;
	import core.actor.player.Dust;
	import core.actor.player.PlayerActor;
	import core.map.BaseMap;
	import core.map.MapTile;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageQuality;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.media.Microphone;
	import flash.net.URLRequest;
	import flash.system.System;
	import flash.text.TextField;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	
	import loader.LoadProxy;
	import loader.core.BaseServer;
	import loader.core.LoadType;
	
	import magics.Magic;
	import magics.MagicSprite;
	
	import manager.LoadManager;
	import manager.SceneManager;
	
	import mx.core.mx_internal;
	
	import ui.scene.MainScene;
	
	[SWF(width="1000",height="560",backgroundColor="#D6E3F7",frameRate="24")]
	public class GameCore extends Sprite
	{
		//private var _tm:TickManager = new TickManager();
		TickManager.instance.start();
		public var curTime:int;//当前时刻
		
		private var _tResData:Array = [];
		
		private var _aniNames:Array = ["stand","11001201","ahead","attack1","attack2",
			"avoid1","avoid2","back","catch","dead","defend","fly1","hit1","hit2",
			"hit3","hp_cast","run","upgrade","vertigo","wait"]
			
			
		private var _packsName:Array = ["m401t000","m402t000"//,"m405t000"
			,"m406t000","m407t000","m408t000","m409t000","m410t000"];
			
		private var _ms:MagicSprite;
		private var _ms2:MagicSprite;
		private var _ms3:MagicSprite;
		private var _ms4:MagicSprite;
		
		private var _player:DisplayObject;
		
		private var _monitortf:TextField = new TextField();
		
		private var bs:BattleScene;
		
		private var _tickTimer:Timer;
		
		private var _m:BaseMap;
			
		public function GameCore()
		{
			//new LoadManager();
			_tickTimer = new Timer(1000/24);
			_tickTimer.addEventListener(TimerEvent.TIMER,customTickHandler);
			_tickTimer.start();
			this.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
			//this.addEventListener(MouseEvent.CLICK,clickHandler);
			this.addEventListener(MouseEvent.CLICK,move);
			
			
			//----------------------------------------------------

			//this.stage.quality = StageQuality.LOW;
			//_bmdContainer.bitmapData = _basicBMD;
			//this.addChild(_bmdContainer);
			
			
			//bs = new BattleScene();
			//this.addChild(bs);
			//bs.init();
			
			
			// MainScenceTest------------------------------------------

			//SceneManager.getInstance().preInit();
			//var mainScene:MainScene = SceneManager.getInstance().mainScene;
			////this.addChild(mainScene);
			//mainScene.initGameMap();

			//TickManager.instance.addObject(mainScene);
			//----------------------------------------------------
			
			
			
			
			var md:MapDefinition = new MapDefinition();
			md.init();
			md.addEventListener(MapDefinition.MAP_DEF_LOAD_COMPLETE,defCompleteHandler);
			md.load("new map","../testRes/map/9999/9999.xml");
	
			/*var mp:MapPack = new MapPack();
			mp.load(name,"../testRes/mapPack/0_0.swf");
			mp.addEventListener(Event.COMPLETE,mpCompleteHandler);*/
			
			/*var t1:MapTile = InstancePool.instance.getMapTile();
			var t2:MapTile = InstancePool.instance.getMapTile();
			var t3:MapTile = InstancePool.instance.getMapTile();
			var t4:MapTile = InstancePool.instance.getMapTile();
			t1.setInfo("0_0",new Point(10,0),"","");
			t2.setInfo("0_1",new Point(10,300),"","");
			t3.setInfo("1_0",new Point(310,0),"","");
			t4.setInfo("1_1",new Point(310,300),"","");
			
			
			this.addChild(t1);
			this.addChild(t2);
			this.addChild(t3);
			this.addChild(t4);*/
			
			_m = new BaseMap();
			_m.init(new MapDefinition());
			_m.initMap(600,600);
			
			this.addChild(_m);
			init();
			//var bmc1:BitmapMovieClip = new BitmapMovieClip("../testRes/arrowLight.swf",0);
			//bmc1.x = 400;
			//bmc1.y = 300;
			//this.addChild(bmc1);
			//var bmc2:BitmapMovieClip = new BitmapMovieClip("../testRes/daoguang.swf",0);
			//bmc2.x = 400;
			//bmc2.y = 300;
			//this.addChild(bmc2);
			
			
			/*_ms = new MagicSprite();
			_ms.initMagic("../testRes/rotate.swf","bmd",new Rectangle(0,0,400,200),true);
			_ms.setProperty("x",400);
			_ms.setProperty("y",300);
			//_ms.setProperty("blendMode",BlendMode.ADD);
			_ms.addMagic(this);
			_ms.play();
	*/
			
			_monitortf.x = 100;
			_monitortf.y = 10;
			_monitortf.width = 100;
			_monitortf.height = 20;
			
			this.addChild(_monitortf);
			
			//LoadPicts();
			
			
			//TickManager.instance.addObject(bmc1);
			//TickManager.instance.addObject(bmc2);
			/*var bmc3:BitmapMovieClip = new BitmapMovieClip("../testRes/rotate.swf",1);
			bmc3.x = 400;
			bmc3.y = 300;
			this.addChild(bmc3);
			*/
			/*var ra:Array = [];
			for(var i:int = 0;i<1000;i++)
			{
				var ref:RefTest1 = new RefTest1();
				ra.push(ref);
			}
			ra.splice(0);
			var t:int = getTimer();
			System.gc();
			trace(getTimer()-t);*/
			this.addChild(new Stats());
			
		}
		
		private var _myMoves:Array=[];
		
		private function move(e:Event):void
		{
			for(var i=0; i<_actors.length;i++)
			{
				_actors[i].doMove();
			}
			_m.move(_m.centerX+10,_m.centerY+10);
			
		}
		
		
		private function mpCompleteHandler(e:Event):void
		{
			var mp:MapPack = (e.target as MapPack);
			var d:Array = mp.data;
			for each(var bmd:BitmapData in d)
			{
				var bm:Bitmap = new Bitmap(bmd);
				bm.x = 600*Math.random();
				bm.y = 400*Math.random();
				this.addChild(bm);
			}
		}
		
		private function defCompleteHandler(e:Event):void
		{
			var md:MapDefinition = e.target as MapDefinition;
			var xml:XML = md.xml;
			
		}
		
		private function packComplete(e:DressPackEvent):void
		{
			var bm:Bitmap = new Bitmap( e.pack.getResource("S_Boot_R"));
			bm.x = 100;
			this.addChild(bm);
			trace(e.pack.getOffset("S_Boot_R").x);
		}
		
	
		
		private var _tempCounter:int = 0;
		private function clickHandler(e:MouseEvent):void
		{
			
			
			
			
			//bs.testPlay(); 110609 注释
			init(100);
			
			/*if(_tempCounter == 5)
			{
				init(100);
				_tempCounter = 0;
			}
			else
			{
				for(var i:int = 0;i<_actors.length;i++)
				{
					var name:String;
					if(_tempCounter == 0)
						name = "4_Attack_1";
					else if(_tempCounter == 1)
						name = "run";
					else if(_tempCounter == 2)
						name = "4_Dodge";
					else if(_tempCounter == 3)
						name = "4_Stand";
					else 
						break;
					_actors[i].doAction(name,true);
				}
				_tempCounter++;
			}*/
			
			
			
			/*if(_ms != null)
				_ms.play();
			if(_ms2 != null)
				_ms2.play();
			if(_ms3 != null)
				_ms3.play();
			if(_ms4 != null)
				_ms4.play();*/
			
			/*_ms = new MagicSprite();
			_ms.initMagic("../testRes/rotate.swf","bmd");
			_ms.setProperty("x",400);
			_ms.setProperty("y",300);
			//_ms.setProperty("blendMode",BlendMode.ADD);
			_ms.addMagic(this);
			_ms.play();
*/
		}
		
		
		
		private var _actions:Array = ["4_Attack_1","4_Stand"];
		private function init(count:int = 1):void
		{
			// test
			//this.stage.addEventListener(KeyboardEvent.KEY_DOWN,SceneManager.getInstance().keyboardManager.keyDownHandler);
			//this.stage.addEventListener(KeyboardEvent.KEY_UP,SceneManager.getInstance().keyboardManager.keyUpHandler);
			
			for(var i:int = 0;i<count;i++)
			{
				var chr:PlayerActor = new PlayerActor();

				chr.initBone(_packsName);// 初始化骨骼数据
				this.addChild(chr);
				addActor(chr);
				chr.doAction(_actions[int(Math.random()*1.9)],true);
				chr.cachePolicy = ActorCachePolicy.Cache_All;
			
				//chr.direction = int(Math.random()*2);
				
				//chr.direction = Math.random()>0.5?0:1;
				//chr.jumpFrame = true;// 是否跳帧
				//var a:TestAction = ActionManager.instance.getAction("TestAction") as TestAction;
				//var aniUrlStr:String = "E:/NewGame/ClientRes/resource/images/ani/100001000/"+_aniNames[int(Math.random()*(_aniNames.length-1))]+".ani";//
				//var aniUrlStr:String  = "C:/motion.xml";
				
				//a.init(chr);
				//a.overwriteAni(aniUrlStr);
				//a.setPropAndPlay(new Point(100,100),5000);
				//a.play();
				//var b:WalkAction = ActionManager.instance.getAction("WalkAction") as WalkAction;
				//b.init(chr);
				//chr.x = Math.random()*800;
				//chr.y = Math.random()*460;
				//var testDust = new Dust("../testRes/Dust.swf");
				//chr.addChild(testDust);
				chr.x = 300;
				chr.y = 100;
				//_player = chr;
				//b.setPropAndPlay(new Point(Math.random()*1000,Math.random()*600),5000);
				
				//chr.setPosition(500,340);
			}
		}
		
		
		//单位图测试
		private var _basicBMD:BitmapData = new BitmapData(1000,700,true,0x00000000);
		private var _bmdContainer:Bitmap = new Bitmap();
		private var _actors:Vector.<PlayerActor> = new Vector.<PlayerActor>();
		private function addActor(actor:PlayerActor):void
		{
			_actors.push(actor);
		}
		
		private function drawActor(canvas:BitmapData):void
		{
			for(var i:int = 0;i<_actors.length;i++)
			{
				//var actorBMD:BitmapData = _actors[i].getBMD();
				//_actors[i].x = 1000*Math.random();
				//_actors[i].y = 700*Math.random();
				//if(actorBMD!=null)
					//canvas.copyPixels(actorBMD,actorBMD.rect,new Point(_actors[i].x,_actors[i].y),actorBMD,new Point(0,0),true);
			}
		}
		
		private function initTestResData():Array
		{
			var tempSprit:Sprite = new Sprite();
			var color:uint =0xFFFFFF*Math.random();
			tempSprit.graphics.beginFill(color);
			tempSprit.graphics.drawRect(0,0,20,20);
			//var bg:BitmapData = new BitmapData(100,100,true,0x00000000);
			//var bmd:BitmapData = new BitmapData(30,30,true,color);
			//var m:Matrix = new Matrix()
			//m.rotate(0.3);
			//m.translate(20,20);
			//bg.draw(bmd,m);
			//var bm:Bitmap = new Bitmap(bg);
			//this.addChild(bm);
			//tempSprit.addChild(bm);
			//this.addChild(tempSprit);
			//tempSprit.rotation = 30;
			//this.graphics.beginBitmapFill(bg,null,false,true);
			//this.graphics.drawRect(0,0,100,100);
			
			var rArr:Array = [];
			rArr["head"] = tempSprit;
			return rArr;
		}
		
		private function customTickHandler(e:TimerEvent):void
		{
			
		}
		
		var counter:int = 0;
		private function enterFrameHandler(e:Event):void
		{
			counter += 10;
			TickManager.instance.updateTick();
			
			//var a:int = SceneManager.getInstance().mainScene.dx;

			//SceneManager.getInstance().mainScene.dx = 300+300*Math.cos(counter*Math.PI/180);
			//SceneManager.getInstance().mainScene.dy = 300+300*Math.sin(counter*Math.PI/180);
			
			drawActor(_basicBMD);
			
			if(bs!=null)
				_monitortf.text = this.bs.combiner.curFrame+"/"+this.bs.combiner.totalFrame+"["+this.bs.combiner.moveFrame+"]"+"["+this.bs.combiner.speed+"]";//this.bs.numChildren.toString();
			else
				_monitortf.text = this.numChildren.toString();
		}
	}
}
